﻿/*文件注释
 * ===============================================================================
 * 说明：
 * 作者：曾永佑
 * 版本：V1.0
 * 创建日期：2013-05-09
 * 完成日期： 
 * 修改纪录：  
 * ===============================================================================
 */  
        
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web; 
using System.Web.Mvc;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System;

namespace SRF.UI.Html
{
    public static class GridExtensions
    {
        /// <summary>
        /// 表格表头
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="text"></param>
        /// <param name="orderField"></param>
        /// <returns></returns>
        public static MvcHtmlString GridHeader(this HtmlHelper htmlHelper, string text, string orderField)
        {
            string html, orderby, sort;
            if (!string.IsNullOrEmpty(orderField))
            {
                HttpRequestBase request = htmlHelper.ViewContext.HttpContext.Request;
                TagBuilder iTag = null;
                UrlBuilder urlBuilder = new UrlBuilder(request);
                string curOrderby = request["order"];
                if (string.IsNullOrEmpty(curOrderby))
                {
                    sort = "asc";
                    orderby = orderField + "_" + sort;
                    urlBuilder.Append("order", orderby);
                }
                else
                {
                    string curOrderField;
                    int _pos = curOrderby.LastIndexOf('_');
                    if (_pos > 0 && _pos < curOrderby.Length - 1)
                    {
                        curOrderField = curOrderby.Substring(0, _pos);
                        sort = curOrderby.Substring(_pos + 1).ToLower();
                    }
                    else
                    {
                        curOrderField = curOrderby;
                        sort = "";
                    }
                    if (curOrderField.ToLower() == orderField.ToLower())
                    {
                        iTag = new TagBuilder("i");
                        iTag.MergeAttribute("class", sort == "asc" ? "icon-arrow-up" : "icon-arrow-down");
                        sort = sort == "desc" ? "asc" : "desc";
                    }
                    else
                    {
                        sort = "asc";
                    }
                    orderby = orderField + "_" + sort;

                    urlBuilder["order"] = orderby;
                }

                TagBuilder aTag = new TagBuilder("a");
                aTag.MergeAttribute("href", urlBuilder.ToString());
                aTag.SetInnerText(text);
                html = string.Format("<th>{0}{1}</th>", aTag.ToString(), iTag == null ? "" : iTag.ToString());
            }
            else
            {
                html = string.Format("<th>{0}</th>", text);
            }

            return new MvcHtmlString(html);
        }
    }
}